/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_system.parser;

import test_system.questions.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @author root
 */
public class ParseRefactorForTeacher extends ParseRefactor {

    enum level {easy, normal, hard}

    ;


    private Answer[] createAnswerVars(String[] questionData) {
        Answer rightAnswer = new Answer(questionData[1], true);
        Answer[] answerVars = new Answer[questionData.length - 1];
        answerVars[0] = rightAnswer;
        for (int i = 2; i < questionData.length; i++) {
            answerVars[i - 1] = new Answer(questionData[i], false);
        }
        return answerVars;

    }

    public TestWrapper createTest() {
        ArrayList<QuestionWrapper> testList = new ArrayList<QuestionWrapper>();
        for (level questionFileLevel : level.values()) {
            StringBuffer testDataBuffer = new ParserCSV().parseFile(questionFileLevel.toString() + ".txt");
            String[] testData = testDataBuffer.toString().split(";");

            for (int i = 0; i < testData.length; i++) {
                String[] questionData = testData[i].split(",");
                if (questionData.length == 2) {
                    Answer rightAnswer = new Answer(questionData[1], true);
                    testList.add(new QuestionWrapper(new TextAnswerQuestion(questionData[0], rightAnswer)));
                }
                Answer[] answerVars = createAnswerVars(questionData);
                testList.add(new QuestionWrapper(new CheckAnswerQuestion(questionData[0], answerVars, questionFileLevel.toString())));
            }
        }
        return new TestWrapper(testList.toArray(new QuestionWrapper[0]));
    }

    public TestWrapper createTest(String file) {
        ArrayList<QuestionWrapper> testList = new ArrayList<QuestionWrapper>();
        StringBuffer testDataBuffer = parseFile(file);
        String[] testData = testDataBuffer.toString().split(";");
        String[] info = new String[2];
        for (int i = 0; i < testData.length; i++) {
            String[] questionData = testData[i].split("_");
            info[0] = questionData[0];
            info[1] = questionData[0];

            Answer[] answerVars = new Answer[questionData.length - 3];
            answerVars[0] = new Answer(questionData[3], true);

            for (int j = 4; j < questionData.length; j++) {
                answerVars[j - 3] = new Answer(questionData[j], false);
            }
            testList.add(new QuestionWrapper(new CheckAnswerQuestion(questionData, answerVars)));
        }
        return new TestWrapper(testList.toArray(new QuestionWrapper[0]));
    }

    public StringBuffer parseFile(String fileName) {
        StringBuffer parseResult = new StringBuffer();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
            String line = "";
            StringTokenizer token = null;
            int index = 0;
            while ((line = bufferedReader.readLine()) != null) {
                token = new StringTokenizer(line, "_");
                while (token.hasMoreTokens()) {
                    parseResult.append(token.nextToken() + "_");
                }
                parseResult.append(";");
            }
        } catch (Exception e) {
            System.out.println("Check your file.");
            System.err.println("Parse Error: " + e.getMessage());
            System.out.println("The programm will close!");
            System.exit(0);
        }
        return parseResult;
    }
}




